/******************************************************************************
* File description: BaseTextInput.qml
* Function: 单行输入框基类
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-07
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14

Item {
    id: root

    /* property 定义开始 */
    // 输入框
    property bool isEnable: true
    property string inputBgColor: "#FFF"
    property int inputBorderWidth: 1
    property string inputBorderColor: "#999"
    property int inputRadius: 4
    property int inputTopPadding: 0
    property int inputBottomPadding: 0
    property int inputLeftPadding: 13
    property int inputRightPadding: 13
    property int inputPadding: 0
    property bool isReadOnly: false
    property int inputEchoMode: TextInput.Normal
    property int inputCursor: Qt.IBeamCursor
    property string inputText: ""
    property QtObject validator: null

    // 文字
    property string inputFontFamily: "Alibaba PuHuiTi 3.0"
    property int inputFontPixelSize: 17
    property string inputFontColor: "#222"
    property int inputFontWeight: Font.Normal
    property int inputHAlign: TextInput.AlignLeft
    property int inputVAlign: TextInput.AlignVCenter

    // placeholder
    property string placeholderText: "--Default Placeholder--"
    property string placeholderColor: "#FF999999"

    // 禁用状态
    property string disabledBgColor: "#F5F5F5"
    property string disabledBorderColor: "#999"
    property string disabledFontColor: "#BFBFBF"
    property int disabledCursor: Qt.ArrowCursor

    // 信号
    signal textEdited(string text);
    signal editingFinished(string text);
    signal acceptableInputChanged(bool acceptableInput);

    // 函数
    // 设置文本内容
    function setInputText(text){
        input.text = text;
    }

    // 清空文本框
    function clearTextInput(){
        input.clear()
    }

    // 设置inputMask
    function setInputMask(inputMask){
        input.inputMask = inputMask
    }

    /* property 定义结束 */

    Rectangle {
        anchors.fill: parent
        color: isReadOnly ? disabledBgColor : inputBgColor
        border.width: inputBorderWidth
        border.color: isReadOnly ? disabledBorderColor : inputBorderColor
        radius: inputRadius

        TextInput {
            id: input
            anchors.fill: parent
            font.pixelSize: inputFontPixelSize
            font.family: inputFontFamily
            font.weight: inputFontWeight
            color: isReadOnly ? disabledFontColor : inputFontColor
            readOnly: isReadOnly
            activeFocusOnPress: true
            echoMode: inputEchoMode
            clip: true
            topPadding: inputTopPadding
            bottomPadding: inputBottomPadding
            leftPadding: inputLeftPadding
            rightPadding: inputRightPadding
            padding: inputPadding
            horizontalAlignment: inputHAlign
            verticalAlignment: inputVAlign
            text: root.inputText
            validator: root.validator

            // 信号
            onTextEdited: root.textEdited(text)
            onEditingFinished: root.editingFinished(text)
            onAcceptableInputChanged: root.acceptableInputChanged(acceptableInput)

            // placeholder
            Label {
                visible: parent.displayText.length === 0
                anchors.fill: parent
                font: parent.font
                color: placeholderColor
                text: qsTr(placeholderText)
                topPadding: parent.topPadding
                bottomPadding: parent.bottomPadding
                leftPadding: parent.leftPadding
                rightPadding: parent.rightPadding
                verticalAlignment: parent.verticalAlignment
                horizontalAlignment: parent.horizontalAlignment
            }

            // MouseArea {
            //     anchors.fill: parent
            //     enabled: isEnable
            //     cursorShape: isEnable ?
            //                      (isReadOnly ? inputCursor : disabledCursor)
            //                    :
            //                      disabledCursor
            //     onClicked: {
            //         input.focus = true
            //     }
            // }
        }
    }
}
